package com.whfc.fvs.service.xxl.job;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.whfc.XxlJobConfig;
import com.whfc.fvs.enums.VsPlatform;
import com.whfc.fvs.service.manager.FvsSnapshotMgr;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.stereotype.Component;

/**
 * @author qzexing
 * @version 1.0
 * @date 2021-02-25 15:15
 */
@Component
@ConditionalOnBean(XxlJobConfig.class)
public class FvsSnapshotJob {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private FvsSnapshotMgr snapshotMgr;

    @XxlJob("snapshotExecute")
    public void execute() {
        try {
            logger.info("视频监控定时抓图任务启动...");
            //获取参数
            String paramStr = XxlJobHelper.getJobParam();
            JSONObject jsonObject = JSONObject.parseObject(paramStr);
            if (jsonObject == null) {
                return;
            }
            JSONArray deptIds = jsonObject.getJSONArray("deptIds");
            if (deptIds != null && deptIds.size() > 0) {
                for (int i = 0; i < deptIds.size(); i++) {
                    Integer deptId = deptIds.getInteger(i);
                    snapshotMgr.createSnapshot(deptId);
                }
            }
        } catch (Exception e) {
            logger.warn("执行定时任务报错.error:{}", e.getMessage());
        }
    }

    @XxlJob("refreshEzvizDeviceSnapshot")
    public void refreshEzvizDeviceSnapshot() {
        try {
            XxlJobHelper.log("更新海康设备视频截图...");
            snapshotMgr.updateDeviceSnapshot(VsPlatform.HIK_EZVIZ);
        } catch (Exception e) {
            XxlJobHelper.handleFail("更新海康设备视频截图失败,error:" + e.getMessage());
        }
    }

    @XxlJob("refreshImouDeviceSnapshot")
    public void refreshImouDeviceSnapshot() {
        try {
            XxlJobHelper.log("更新大华设备视频截图...");
            snapshotMgr.updateDeviceSnapshot(VsPlatform.DAHUA_IMOU);
        } catch (Exception e) {
            XxlJobHelper.handleFail("更新海康设备视频截图失败,error:" + e.getMessage());
        }

    }
}
